package com.jcgyl.export.service.Impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jcgyl.export.dao.DepartmentDao;
import com.jcgyl.export.dao.PostDao;
import com.jcgyl.export.entity.Post;
import com.jcgyl.export.entity.Tbsysuser;
import com.jcgyl.export.entity.ViewPost;
import com.jcgyl.export.mapper.PostMapper;
import com.jcgyl.export.service.PostService;
import com.jcgyl.export.util.entity.Result;

@Service
public class PostServiceImpl implements PostService {
	@Autowired
	private PostDao postDao;
	@Autowired
	private PostMapper postMapper;
	@Autowired
	private DepartmentDao departmentDao;
   
	@Override
	public Map<String, Object> findAllPost(String postName, String departmentName, int pageSize, int pageIndex) {
		int rowCount = postDao.findRowCount(postName, departmentName);
		int startIndex = (pageIndex - 1) * pageSize;
		List<ViewPost> list = postDao.findAllPost(postName, departmentName, pageSize, startIndex);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", list);
		map.put("totalRows", rowCount);
		return map;
	}
    //添加岗位
	@Override
	public Result addPost(ViewPost viewpost) {
		Tbsysuser user=(Tbsysuser)SecurityUtils.getSubject().getSession().getAttribute("user");
		System.out.println("---------------------------------------------->"+user.getUname());
		String departmentName = viewpost.getDepartmentName();
		String postName = viewpost.getPostName();
		System.out.println("前台发送的数据departmentName"+departmentName+"postName"+postName);
		int id = departmentDao.findByDepartmentName(departmentName).getId();
		if (id <= 0) {
			return Result.build(1, "部门名称已存在，请查询后在添加");
		}
		if(postDao.findByPostName(postName)!=null){
			return Result.build(1, "岗位名称已存在，请查询后在添加");
		}
		try {
			Post post=new Post();
			post.setPostName(viewpost.getPostName());
			post.setPostEnglish(viewpost.getPostEnglish());
			post.setpId(id);
			post.setNote(viewpost.getNote());
			post.setCreatedUser(user.getUname());
			post.setCreatedTime(new Date());;
			postMapper.insert(post);
			return Result.ok("添加岗位成功");
		} catch (Exception ex) {
			ex.printStackTrace();
			return Result.build(1, "添加岗位时发生错误");
		}
	}
	//修改岗位
	@Override
	public Result updatePost(ViewPost viewpost) {
		Tbsysuser user=(Tbsysuser)SecurityUtils.getSubject().getSession().getAttribute("user");
		String departmentName = viewpost.getDepartmentName();
		String postName = viewpost.getPostName();
		System.out.println("前台发送的数据departmentName"+departmentName+"postName"+postName);
		int departmentId = departmentDao.findByDepartmentName(departmentName).getId();
		if (departmentId <= 0) {
			return Result.build(1, "部门名称不存在，请查询后在修改");
		}
		try {
			int id= viewpost.getId();
			System.out.println(id);
			Post post=postDao.findPostById(id);
			post.setPostName(viewpost.getPostName());
			post.setPostEnglish(viewpost.getPostEnglish());
			post.setpId(departmentId);
			post.setModifiedUser(user.getUname());
			post.setModifiedTime(new Date());
			postMapper.updateByPrimaryKey(post);
			return Result.ok("修改岗位成功");
		} catch (Exception ex) {
			ex.printStackTrace();
			return Result.build(1, "修改岗位时发生错误");
		}
	}

	/** 删除岗位 */
	@Override
	public Result deletePost(int postId) {
		try {
			if (postId <= 0) {
				return Result.build(1, "岗位名称部存在，请查询后在添加");
			}
			postMapper.deleteByPrimaryKey(postId);
			return Result.ok("删除成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			return Result.build(1, "删除岗位时发生错误");
		}
	}

	/**
	 * 根据部门id查询岗位
	 * JL
	 */
	@Override
	public List<Post> findPost(int departmentId) {
		List<Post> list = postDao.findPostByDepartmentId(departmentId);
		System.out.println(list);
		return list;
	}
	@Override
	public Post findByPostName(String postName) {
		return postDao.findByPostName(postName);
	}
	@Override
	public List<Post> findZtreeObject() {
		return postDao.findPostZtree();
	}
}
